/*******************************************************************************
 * Copyright 2011-2012 Dik Grapendaal
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package sh.grapendaal.tsuushin.tracking;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * Class representing a user on an IRC server.
 */
public class User {
	private String networkName;
	private String nickname;
	private String username;
	private String hostname;
	private String realname;

	/**
	 * This class can only be instantiated from the factory method parse().
	 */
	private User() {
	}

	public String getNetworkName() {
		return networkName;
	}

	public String getNickname() {
		return nickname;
	}

	public String getUsername() {
		return username;
	}

	public String getHostname() {
		return hostname;
	}

	public String getRealname() {
		return realname;
	}
	
	private boolean isNicknameOnly() {
		return username == null;
	}

	/**
	 * Parses the given user string to a User object. This can be a string in
	 * the form of nickname!username@hostname or just a nickname. The latter is
	 * possible when receiving server notices, the full user string will then be
	 * the hostname of the server sending the notice.
	 * 
	 * @param user
	 *            The user string to parse.
	 * @return The resulting User object containing the parsed information.
	 */
	public static User parse(String user) {
		User instance = new User();

		int separator = user.indexOf('!');
		if (separator != -1) {
			String[] chunks = user.split("!|@", 3);
			instance.nickname = chunks[0];
			instance.username = chunks[1];
			instance.hostname = chunks[2];
		} else {
			instance.nickname = user;
		}

		return instance;
	}

	@Override
	public boolean equals(Object object) {
		if (!(object instanceof User)) {
			return false;
		}
		User rhs = (User) object;
		EqualsBuilder equals = new EqualsBuilder().append(this.networkName, rhs.networkName).append(
				this.nickname.toLowerCase(), rhs.nickname.toLowerCase());
		if (!this.isNicknameOnly()) {
			equals.append(this.username.toLowerCase(), rhs.username.toLowerCase()).append(this.hostname.toLowerCase(),
					rhs.hostname.toLowerCase());
		}
		return equals.isEquals();
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder().append(nickname);
		if (!this.isNicknameOnly()) {
			builder.append("!").append(username).append("@").append(hostname);
		}
		return builder.toString();
	}

	@Override
	public int hashCode() {
		HashCodeBuilder hashCode = new HashCodeBuilder(-231742717, -1640978217).append(this.networkName).append(
				this.nickname.toLowerCase());
		if (!this.isNicknameOnly()) {
			hashCode.append(this.username.toLowerCase()).append(this.hostname.toLowerCase());
		}
		return hashCode.toHashCode();
	}
}
